package cn.infozr.api.client;

import java.util.UUID;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 药品标准库接口测试类
 * 用于测试 http://127.0.0.1:8080/ygydapi/v1/getDrugList 接口
 */
public class DrugListApiClient extends BaseClient {
    
    /**
     * 药品数据请求参数类
     */
    public static class DrugListRequest {
        private String appId;
        private String timestamp;
        private String reqId;
        private String updateTime;
        private Integer size;
        private String sign;
        
        // 构造函数
        public DrugListRequest() {
            this.timestamp = String.valueOf(System.currentTimeMillis());
            this.reqId = UUID.randomUUID().toString().replace("-", "");
            this.size = 500;
        }
        
        // Getters and Setters
        public String getAppId() { return appId; }
        public void setAppId(String appId) { this.appId = appId; }
        
        public String getTimestamp() { return timestamp; }
        public void setTimestamp(String timestamp) { this.timestamp = timestamp; }
        
        public String getReqId() { return reqId; }
        public void setReqId(String reqId) { this.reqId = reqId; }
        
        public String getUpdateTime() { return updateTime; }
        public void setUpdateTime(String updateTime) { this.updateTime = updateTime; }
        
        public Integer getSize() { return size; }
        public void setSize(Integer size) { this.size = size; }
        
        public String getSign() { return sign; }
        public void setSign(String sign) { this.sign = sign; }
    }
    
    /**
     * 调用获取药品列表接口
     * @param updateTime 最后更新时间，可为null
     * @param size 返回数据条数，可为null
     * @return 接口响应结果
     * @throws Exception 网络或解析异常
     */
    public String getDrugList(String updateTime, Integer size) throws Exception {
        // 构造请求参数
        DrugListRequest request = new DrugListRequest();
        request.setAppId(APP_ID);
        request.setTimestamp(String.valueOf(System.currentTimeMillis()));
        if (updateTime != null) {
            request.setUpdateTime(updateTime);
        }
        if (size != null) {
            request.setSize(size);
        }
        
        // 生成签名
        request.setSign(generateSign(request));
        
        // 发送POST请求
        return sendPostRequest(API_URL_FRUG_LIST, request);
    }
    

    
    /**
     * 解析并打印药品数据
     * @param jsonResponse JSON响应字符串
     * @throws Exception 解析异常
     */
    public void parseAndPrintDrugData(String jsonResponse) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode rootNode = objectMapper.readTree(jsonResponse);
        int code = rootNode.get("code").asInt();
        if(code==0) {
            JsonNode dataArray = rootNode.get("data");
            if (dataArray != null && dataArray.isArray()) {
                System.out.println("药品数据条数: " + dataArray.size());
                for (JsonNode drugNode : dataArray) {
                    System.out.println("--- 药品信息 ---");
                    System.out.println("药品唯一标识码: " + (drugNode.has("ypwybsm") ? drugNode.get("ypwybsm").asText() : ""));
                    System.out.println("药品追溯码前7位: " + drugNode.get("ypbsm").asText());
                    System.out.println("药品外包装上的条形码: " + drugNode.get("yptxm").asText());
                    System.out.println("药品通用名称: " + (drugNode.has("yptymc") ? drugNode.get("yptymc").asText() : ""));
                    System.out.println("药品通用名称拼音首字母: " + (drugNode.has("yppyszm") ? drugNode.get("yppyszm").asText() : ""));
                    System.out.println("药品批准文号: " + (drugNode.has("yppzwh") ? drugNode.get("yppzwh").asText() : ""));
                    System.out.println("上市许可持有人名称: " + (drugNode.has("ypssxkcyrmc") ? drugNode.get("ypssxkcyrmc").asText() : ""));
                    System.out.println("药品生产单位名称: " + (drugNode.has("ypscqymc") ? drugNode.get("ypscqymc").asText() : ""));
                    System.out.println("药品注册分类: " + drugNode.get("ypzcfl").asText());
                    System.out.println("特殊药品管理分类: " + drugNode.get("tsypglfl").asText());
                    System.out.println("处方药标识: " + (drugNode.has("cfybs") ? drugNode.get("cfybs").asText() : ""));
                    System.out.println("是否医保名录药品: " + (drugNode.has("ybmlyp") ? drugNode.get("ybmlyp").asText() : ""));
                    System.out.println("国产或进口药品标识: " + (drugNode.has("gchjkyp") ? drugNode.get("gchjkyp").asText() : ""));
                    System.out.println("药品分类: " + (drugNode.has("ypfl") ? drugNode.get("ypfl").asText() : ""));
                    System.out.println("药品有效期: " + (drugNode.has("ypyxq") ? drugNode.get("ypyxq").asText() : ""));
                    System.out.println("药品有效期单位: " + (drugNode.has("ypyxqdw") ? drugNode.get("ypyxqdw").asText() : ""));
                    System.out.println("包装规格: " + (drugNode.has("bzgg") ? drugNode.get("bzgg").asText() : ""));
                    System.out.println("制剂规格: " + (drugNode.has("zjgg") ? drugNode.get("zjgg").asText() : ""));
                    System.out.println("剂型: " + (drugNode.has("jx") ? drugNode.get("jx").asText() : ""));
                    System.out.println("用药禁忌: " + drugNode.get("yyjj").asText());
                    System.out.println("特殊人群: " + drugNode.get("tsrq").asText());
                    System.out.println("功能主治: " + drugNode.get("gnzz").asText());
                    System.out.println("用法用量: " + drugNode.get("yfyl").asText());
                    System.out.println("药品性状: " + drugNode.get("ypxz").asText());
                    System.out.println("不良反应: " + drugNode.get("blfs").asText());
                    System.out.println("储藏条件: " + drugNode.get("cctj").asText());
                    System.out.println("注意事项: " + drugNode.get("zysx").asText());
                    System.out.println("相互作用: " + drugNode.get("xhzy").asText());
                    System.out.println("数据更新时间: " + (drugNode.has("updateTime") ? drugNode.get("updateTime").asText() : ""));
                    System.out.println("------------------");
                }
            }
        }
    }
    
    /**
     * 主方法 - 测试接口调用
     */
    public static void main(String[] args) {
        try {
            DrugListApiClient client = new DrugListApiClient();
            String updateTime="2025-08-01 09:37:30.344";//本地缓存上一次最大值
            // 测试调用接口
            String result = client.getDrugList(updateTime, 2); // 获取10条数据
            
            System.out.println("接口响应:");
            System.out.println(result);
            
            // 解析并打印数据
            client.parseAndPrintDrugData(result);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}